﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CheemsControl.Controls
{
    public class ColorPicker : UserControl
    {

        public ColorPicker()
        {
            this.FontSize = 20;
            this.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
            this.FontWeight = FontWeights.Bold;
            // 订阅 Loaded 事件，确保控件加载时触发一次 OnSourceChanged
            this.Loaded += ColorPicker_Loaded;
        }

        private void ColorPicker_Loaded(object sender, RoutedEventArgs e)
        { 
            // 手动触发 OnSourceChanged 方法
            OnSourceChanged(this, new DependencyPropertyChangedEventArgs(SourceProperty, null, Source));
        }

        #region 色块属性



        /// <summary>
        /// 色块宽度
        /// </summary>
        public double ColorBlocksWidth
        {
            get { return (double)GetValue(ColorBlocksWidthProperty); }
            set { SetValue(ColorBlocksWidthProperty, value); }
        }
        public static readonly DependencyProperty ColorBlocksWidthProperty =
            DependencyProperty.Register("ColorBlocksWidth", typeof(double), typeof(ColorPicker), new PropertyMetadata(30.0));


        /// <summary>
        /// 色块高度
        /// </summary>
        public double ColorBlocksHeight
        {
            get { return (double)GetValue(ColorBlocksHeightProperty); }
            set { SetValue(ColorBlocksHeightProperty, value); }
        }
        public static readonly DependencyProperty ColorBlocksHeightProperty =
            DependencyProperty.Register("ColorBlocksHeight", typeof(double), typeof(ColorPicker), new PropertyMetadata(30.0));



        public CornerRadius ColorBlocksCornerRadius
        {
            get { return (CornerRadius)GetValue(ColorBlocksCornerRadiusProperty); }
            set { SetValue(ColorBlocksCornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty ColorBlocksCornerRadiusProperty =
            DependencyProperty.Register("ColorBlocksCornerRadius", typeof(CornerRadius), typeof(ColorPicker), new PropertyMetadata(new CornerRadius(3)));



        #endregion


        /// <summary>
        /// 是否打开动画
        /// </summary>
        public bool IsAnimationEnabled
        {
            get { return (bool)GetValue(IsAnimationEnabledProperty); }
            set { SetValue(IsAnimationEnabledProperty, value); }
        }
        public static readonly DependencyProperty IsAnimationEnabledProperty =
            DependencyProperty.Register("IsAnimationEnabled", typeof(bool), typeof(ColorPicker), new PropertyMetadata(false));

        /// <summary>
        /// 所有可选颜色
        /// </summary>
        public ObservableCollection<Brush> Source
        {
            get { return (ObservableCollection<Brush>)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ObservableCollection<Brush>), typeof(ColorPicker), 
                new PropertyMetadata(new ObservableCollection<Brush>()
                {
                    new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF,0xEA,0x42,0x35)), // 谷歌红
                    new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF,0xFB,0xBC,0x05)), // 谷歌黄
                    new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF,0x4B,0xB1,0x5D)), // 谷歌绿
                    new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF,0x42,0x86,0xF5)), // 谷歌蓝
                    new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF,0xF1,0xF1,0xF1)), // 浅灰
                }, OnSourceChanged));



        /// <summary>
        /// 选中的颜色
        /// </summary>
        public Brush SelectedColor
        {
            get { return (Brush)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(ColorPicker), new PropertyMetadata(null));



        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }
        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(ColorPicker), new PropertyMetadata(0));




        /// <summary>
        /// 素材颜色改变
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPicker thisControl = (ColorPicker)d;
            WrapPanel wrapPanel = new WrapPanel();
            if (thisControl.Source == null ||  thisControl.Source.Count == 0)
            {
                return;
            }
            for (int i = 0; i < thisControl.Source.Count; i++)
            {
                int num = i;
                MessageReminderRadioButton messageReminderRadioButton = new MessageReminderRadioButton()
                {
                    Content = null,
                    FontSize = thisControl.FontSize,
                    Foreground = thisControl.Foreground,
                    FontWeight = thisControl.FontWeight,
                    BorderThickness = new Thickness(0),
                    Background = thisControl.Source[num],
                    MinWidth = thisControl.ColorBlocksWidth,
                    MinHeight = thisControl.ColorBlocksHeight,
                    Margin = new Thickness(2),
                    CornerRadius = thisControl.ColorBlocksCornerRadius,
                    IsAnimationEnabled = thisControl.IsAnimationEnabled,
                };
                messageReminderRadioButton.Checked += (sender, e) =>
                {
                    RadioButton radioButton = sender as RadioButton;
                    if (radioButton != null)
                    {
                        radioButton.Content = "√";  // 选中时修改 Content 为 "√"
                        thisControl.SelectedColor = thisControl.Source[num];
                        thisControl.SelectedIndex = num;
                    }
                };
                messageReminderRadioButton.Unchecked += (sender, e) =>
                {
                    RadioButton radioButton = sender as RadioButton;
                    if (radioButton != null)
                    {
                        radioButton.Content = string.Empty;  // 取消选中时修改为空
                    }
                };
                if (thisControl.SelectedIndex == num)
                {
                    messageReminderRadioButton.IsChecked = true;
                }
                wrapPanel.Children.Add(messageReminderRadioButton);
            }
            thisControl.Content = wrapPanel;
        }

    }
}
